from DEFlib import *
import ColorLib
import pygame
import time
import sys
import random
import math
import os
def Game(win, verson, WINTURN):
    # 初始化全局变量
    ALLCOLOR = ColorLib.AllColor()
    # 窗口和棋盘的配置常量
    WINDOW_SIZE = (1050, 665)
    BOARD_SIZE = 30
    CELL_SIZE = 20
    BOARD_OFFSET = 200

    if WINTURN == 1:
        WINCOLOR = (235, 85, 90)

    elif WINTURN == 2:
        WINCOLOR = (0, 162, 232)

    else:
        WINCOLOR = (255, 255, 255)

    # 初始化字体配置
    if True:
        FONT1 = resource_path('Assets\\yhxmh.ttf')
        FONT2 = resource_path('Assets\\zhbtt.ttf')
        pygame.font.init()
        font = pygame.font.Font(FONT1, 15)
        almidfont = pygame.font.Font(FONT1, 20)
        midfont = pygame.font.Font(FONT1, 25)
        bigfont = pygame.font.Font(FONT2, 35)

    # 预制文字位图
    P1text = font.render("P1", True, ALLCOLOR.black)
    P1text_rect = P1text.get_rect(center=(WINDOW_SIZE[0] / 2, WINDOW_SIZE[1] / 2))
    P2text = font.render("P2", True, ALLCOLOR.black)
    P2text_rect = P2text.get_rect(center=(WINDOW_SIZE[0] / 2, WINDOW_SIZE[1] / 2))
    P3text = font.render("P1和P2", True, ALLCOLOR.black)
    P3text_rect = P3text.get_rect(center=(WINDOW_SIZE[0] / 2, WINDOW_SIZE[1] / 2))
    P1wintext = bigfont.render("玩家1胜利", True, ALLCOLOR.white)
    P2wintext = bigfont.render("玩家2胜利", True, ALLCOLOR.white)

    # 左侧提示文字
    tiptext0_1 = midfont.render("欢迎来到连续棋！", True, ALLCOLOR.white)
    tiptext0_2 = font.render("鼠标移至此处查看游戏规则。", True, (110, 135, 255))
    tiptext1_1 = font.render("当一方玩家棋数超过对方3倍", True, ALLCOLOR.white)
    tiptext1_2 = font.render("或对方场上无棋子时，这名玩", True, ALLCOLOR.white)
    tiptext1_3 = font.render("家胜利。", True, ALLCOLOR.white)
    tiptext2_1 = font.render("浅色区域是对应颜色玩家可以", True, ALLCOLOR.white)
    tiptext2_2 = font.render("选择行动的位置，紫色位置双", True, ALLCOLOR.white)
    tiptext2_3 = font.render("方都可以选择行动。", True, ALLCOLOR.white)
    tiptext3_1 = font.render("当场上出现没有与墙壁相连或", True, ALLCOLOR.white)
    tiptext3_2 = font.render("通过同阵营方棋子与棋盘边缘", True, ALLCOLOR.white)
    tiptext3_3 = font.render("间接相连的棋子时，这部分棋", True, ALLCOLOR.white)
    tiptext3_4 = font.render("子会被消除。", True, ALLCOLOR.white)
    tiptext4_1 = font.render("玩家的得分由棋子的数量与消", True, ALLCOLOR.white)
    tiptext4_2 = font.render("除对手棋子数量决定。每落下", True, ALLCOLOR.white)
    tiptext4_3 = font.render("一颗棋子或消除对方一颗棋子", True, ALLCOLOR.white)
    tiptext4_4 = font.render("获得一分。", True, ALLCOLOR.white)
    tiptext5_1 = font.render("每名玩家每一回合可以行动2", True, ALLCOLOR.white)
    tiptext5_2 = font.render("次,可以选择落子或使用道具。", True, ALLCOLOR.white)
    tiptext5_3 = font.render("使用道具需要消耗得分。被黄", True, ALLCOLOR.white)
    tiptext5_4 = font.render("色框选中的道具为该回合可以", True, ALLCOLOR.white)
    tiptext5_5 = font.render("使用的道具。每当有人使用道", True, ALLCOLOR.white)
    tiptext5_6 = font.render("具后有概率会轮回道具种类。", True, ALLCOLOR.white)
    tiptext6_1 = font.render("详细规则与机制按空格键查看", True, (110, 135, 255))

    dgtext1_1 = midfont.render("特殊道具", True, ALLCOLOR.white)
    dgtext2_1 = almidfont.render("你好？BOOM！", True, (110, 135, 255))
    dgtext2_2 = font.render("消耗 10 点得分", True, ALLCOLOR.white)
    dgtext2_3 = font.render("使选定块左上、右上、左下", True, ALLCOLOR.white)
    dgtext2_4 = font.render("、右下的块扣除5点血量", True, ALLCOLOR.white)
    dgtext3_1 = almidfont.render("彩虹！到四方！", True, (110, 135, 255))
    dgtext3_2 = font.render("消耗 30 点得分", True, ALLCOLOR.white)
    dgtext3_3 = font.render("向选定块上下左右方向反转", True, ALLCOLOR.white)
    dgtext3_4 = font.render("遇到的所有血量不大于1的", True, ALLCOLOR.white)
    dgtext3_5 = font.render("敌方棋子，否则扣除该格1", True, ALLCOLOR.white)
    dgtext3_6 = font.render("点血量直到遇到我方棋子或", True, ALLCOLOR.white)
    dgtext3_7 = font.render("棋盘边缘为止。", True, ALLCOLOR.white)
    dgtext4_1 = almidfont.render("不撞南墙！", True, (110, 135, 255))
    dgtext4_2 = font.render("消耗 4 点得分", True, ALLCOLOR.white)
    dgtext4_3 = font.render("在选定快生成一个有5点血", True, ALLCOLOR.white)
    dgtext4_4 = font.render("量的墙壁，效果可以叠加。", True, ALLCOLOR.white)

    timetext = midfont.render("游戏已开始：", True, ALLCOLOR.white)
    versontext_1 = font.render("当前版本号：", True, ALLCOLOR.white)
    versontext_2 = font.render("·" + verson, True, ALLCOLOR.white)
    zstext = font.render("支持一下", True, (110, 135, 255))
    # 右侧栏对局数据
    dgrtext0_1 = midfont.render("对局数据", True, ALLCOLOR.white)
    dgrtext1_1 = font.render("玩家1本局共行动0步", True, ALLCOLOR.white)
    dgrtext1_2 = font.render("玩家2本局共行动0步", True, ALLCOLOR.white)
    # 准备提示文字
    tiptext1 = "请使用Q键确认玩家1的下棋位置，玩家1使用WASD键移动，使用E键使用特殊道具"
    tiptext2 = "请使用Ctrl键确认玩家2的下棋位置，玩家2使用方向键移动，使用Shift键使用特殊道具"
    r_text_1 = midfont.render("玩家数据", True, ALLCOLOR.white)
    r_text_2 = font.render("P1血量：(也请关注倍数)", True, ALLCOLOR.white)
    r_text_3 = font.render("P2血量：(也请关注倍数)", True, ALLCOLOR.white)
    r_text_4 = font.render("P1得分：", True, ALLCOLOR.white)
    r_text_5 = font.render("P2得分：", True, ALLCOLOR.white)
    # 设置键盘重复
    pygame.key.set_repeat(200, 70)
    # 设置窗口图标
    icon = pygame.image.load(resource_path('Assets\\favicon.ico'))
    pygame.display.set_icon(icon)
    # 带有透明度的第2个窗口表面
    transparent_surface = pygame.Surface((800, 600), pygame.SRCALPHA)
    # 带有透明度的棋盘附加表面
    chessboard_surface = pygame.Surface((WINDOW_SIZE[0], WINDOW_SIZE[1]), pygame.SRCALPHA)
    # pygame.mouse.set_visible(False)
    # 游戏结束时用的表面
    end_surface = pygame.Surface((WINDOW_SIZE[0], WINDOW_SIZE[1]), pygame.SRCALPHA)

    # 初始化游戏状态变量
    running = False
    map = [[0 for _ in range(BOARD_SIZE)] for _ in range(BOARD_SIZE)]
    lifemap = [[0 for _ in range(BOARD_SIZE)] for _ in range(BOARD_SIZE)]
    WALLS = [[0 for _ in range(BOARD_SIZE)] for _ in range(BOARD_SIZE)]
    checkresult = map.copy()
    map[0][0] = 1
    map[BOARD_SIZE - 1][BOARD_SIZE - 1] = 2
    lifemap[0][0] = 1
    lifemap[BOARD_SIZE - 1][BOARD_SIZE - 1] = 1
    turn = 0
    allturn = 1
    player1_place = [1, 1]
    showplayer1_place = player1_place.copy()
    relplayer1_place = player1_place.copy()
    player2_place = [BOARD_SIZE - 2, BOARD_SIZE - 2]
    showplayer2_place = player2_place.copy()
    relplayer2_place = player2_place.copy()
    chessnum = [1, 1]
    BlueCanChessList = []
    RedCanChessList = []
    dglist = [10, 30, 4]
    dgnum = random.randint(0, len(dglist) - 1)
    chessnumratio1 = 1
    chessnumratio2 = 1
    showratio1 = 1
    showratio2 = 1
    player1score = 0
    player2score = 0
    player1maxscore = 1
    player2maxscore = 1
    showplayer1score = 1
    showplayer2score = 1
    player1life = 1
    player2life = 1
    showplayer1life = 0
    showplayer2life = 0
    player1maxlife = 1
    player2maxlife = 1
    player1action = 2
    player2action = 2
    player1allaction = 0
    player2allaction = 0
    chessablealpha = 0
    showchessablealpha = 0
    chessalphatime = 0
    showrandomalpha = 255
    showrandomx = 275
    showrandomy = 150
    showrandomwidth = 500
    showrandomheight = 300
    showlineslong = 0
    againbuttonwidth = 180
    againbuttonheight = 30
    showagainbuttonwidth = 180
    showagainbuttonheight = 30
    dgplace = 0
    showdgplace = 0
    player1alltime = 0
    player2alltime = 0

    changeturnwinwidth = 1050 + 5
    showchangeturnwinwidth = -5

    WINTURN = 0

    chessnumtextimg1 = font.render('玩家1当前棋数 ' + str(chessnum[0]), True, ALLCOLOR.red)
    chessnumtextimg2 = font.render('玩家2当前棋数 ' + str(chessnum[1]), True, ALLCOLOR.blue)
    tiptext1 = "请使用Q键确认玩家1的下棋位置，玩家1使用WASD键移动，使用E键使用特殊道具"
    tiptext2 = "请使用Ctrl键确认玩家2的下棋位置，玩家2使用方向键移动，使用Shift键使用特殊道具"

    heartimg = pygame.image.load(resource_path('Assets\\heart.png')).convert_alpha()
    heartimg = pygame.transform.scale(heartimg, (20, 20))
    heartimg.set_colorkey(ALLCOLOR.white)

    starimg = pygame.image.load(resource_path('Assets\\star.png')).convert_alpha()
    starimg = pygame.transform.scale(starimg, (20, 20))
    starimg.set_colorkey(ALLCOLOR.white)

    zs = pygame.image.load(resource_path('Assets\\zs.png')).convert()
    zs_big = pygame.transform.smoothscale(zs, (200, 228))

    vs = pygame.image.load(resource_path('Assets\\vs.png')).convert()
    vs = pygame.transform.smoothscale(vs, (30, 30))
    vs.set_colorkey(ALLCOLOR.white)

    boom = pygame.image.load(resource_path('Assets\\boom.png')).convert_alpha()
    boom = pygame.transform.smoothscale(boom, (40, 40))

    rainbow = pygame.image.load(resource_path('Assets\\rainbow.png')).convert_alpha()
    rainbow = pygame.transform.smoothscale(rainbow, (40, 40))

    box = pygame.image.load(resource_path('Assets\\box.png')).convert_alpha()
    box = pygame.transform.smoothscale(box, (40, 40))

    sm1 = pygame.image.load(resource_path('Assets\\sm1.png')).convert_alpha()
    sm2 = pygame.image.load(resource_path('Assets\\sm2.png')).convert_alpha()

    def is_valid_position(x, y):
        """
        检查坐标是否在棋盘范围内。

        参数:
        x -- 横坐标
        y -- 纵坐标

        返回:
        布尔值，如果坐标有效则为True，否则为False。
        """
        """检查坐标是否在棋盘范围内"""
        return 0 <= x < BOARD_SIZE and 0 <= y < BOARD_SIZE

    def get_neighbors(x, y, map, turn):
        """
        获取指定位置周围同色棋子的数量。

        参数:
        x -- 横坐标
        y -- 纵坐标
        map -- 棋盘状态数组
        turn -- 当前下棋方

        返回:
        同色棋子的数量。
        """
        """获取与给定位置同色的相邻棋子数量"""
        numsum = 0
        directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]
        for dx, dy in directions:
            nx, ny = x + dx, y + dy
            if is_valid_position(nx, ny) and map[nx][ny] == turn:
                numsum += 1
        return numsum

    def ConsecutiveChessCheck(map):
        """
        检查棋盘上是否存在连续的同色棋子。

        参数:
        map -- 棋盘状态数组

        返回:
        孤立的棋子位置列表。
        """
        rows, cols = len(map), len(map[0])
        visited = [[False for _ in range(cols)] for _ in range(rows)]
        isolated_pieces = []

        def dfs(x, y, color):
            """
            深度优先搜索，标记连通的同色棋子。

            参数:
            x -- 横坐标
            y -- 纵坐标
            color -- 棋子颜色
            """
            if x < 0 or x >= rows or y < 0 or y >= cols or map[x][y] != color or visited[x][y]:
                return
            visited[x][y] = True
            for dx, dy in ((0, 1), (1, 0), (0, -1), (-1, 0)):
                dfs(x + dx, y + dy, color)

        for i in range(rows):
            for j in [0, cols - 1]:
                if map[i][j] in [1, 2]:
                    dfs(i, j, map[i][j])
        for j in range(cols):
            for i in [0, rows - 1]:
                if map[i][j] in [1, 2]:
                    dfs(i, j, map[i][j])

        for i in range(rows):
            for j in range(cols):
                if map[i][j] in [1, 2] and not visited[i][j]:
                    isolated_pieces.append((i, j))

        return isolated_pieces

    # 预处理
    # 获取玩家1能够下棋的坐标并保存到列表BlueCanChessList中
    BlueCanChessList = []
    for i in range(BOARD_SIZE):
        for j in range(BOARD_SIZE):
            if map[i][j] in [0]:
                numsum = get_neighbors(i, j, map, 2)
                if numsum >= 1:
                    BlueCanChessList.append([i, j])
    # 获取玩家2能够下棋的坐标并保存到列表RedCanChessList中
    RedCanChessList = []
    for i in range(BOARD_SIZE):
        for j in range(BOARD_SIZE):
            if map[i][j] in [0]:
                numsum = get_neighbors(i, j, map, 1)
                if numsum >= 1:
                    RedCanChessList.append([i, j])

    time_start = time.time()
    showrandomstarttime = time.time()

    clock = pygame.time.Clock()

    playing = True
    ENDTHISTURN = False
    WIN = False

    # 主游戏循环
    while playing:
        transparent_surface.fill((0, 0, 0, 0))

        # 记录玩家最高分数
        player1maxscore = max(player1score, player1maxscore)
        player2maxscore = max(player2score, player2maxscore)

        # 记录玩家最高血量
        player1maxlife = max(player1life, player1maxlife)
        player2maxlife = max(player2life, player2maxlife)

        keys = pygame.key.get_pressed()

        # 处理事件，如退出或点击
        for event in pygame.event.get():
            pygame.event.pump()

            chessdown = 0

            if event.type == pygame.QUIT:
                del map
                del checkresult
                pygame.quit()
                sys.exit()

            elif event.type == pygame.KEYDOWN and running:
                if event.key == pygame.K_q or event.key == pygame.K_e or event.key == pygame.K_RCTRL or \
                        event.key == pygame.K_LCTRL or event.key == pygame.K_RSHIFT or event.key == pygame.K_LSHIFT:
                    # 玩家1使用Q键
                    if event.key == pygame.K_q and turn == 1:  # 玩家1使用 Q 键
                        # 确保 player1_place 已经定义并包含了玩家1想要下棋的位置
                        place = player1_place[:]

                        if is_valid_position(place[0], place[1]):
                            # 检查位置是否有效且该位置没有被占据
                            numsum = get_neighbors(place[0], place[1], map, turn)
                            if numsum >= 1 or map[place[0]][place[1]] == 1:
                                if player1action > 0:
                                    if map[place[0]][place[1]] == 2:
                                        player2life -= 1

                                        if lifemap[place[0]][place[1]] > 0:
                                            lifemap[place[0]][place[1]] -= 1

                                    elif map[place[0]][place[1]] == 1:
                                        lifemap[place[0]][place[1]] += 1

                                        player1life += 1

                                        tiptext1 = "玩家1巩固了位于(" + str(place[0]) + "," + str(place[1]) + ")的棋子"

                                    elif map[place[0]][place[1]] == 0:
                                        map[place[0]][place[1]] = turn
                                        lifemap[place[0]][place[1]] = 1

                                        player1life += 1

                                        tiptext1 = "玩家1在(" + str(place[0]) + "," + str(place[1]) + ")下棋"

                                    player1score += 1
                                    player1allaction += 1

                                    chessdown = 1

                                    player1action -= 1

                                    chessablealpha = 255

                                    nownum = dgnum
                                    while dgnum == nownum:
                                        dgnum = random.randint(0, len(dglist) - 1)

                                    checkresult = ConsecutiveChessCheck(map)
                                    player1score += len(checkresult)
                                    player2life -= len(checkresult)
                                    for piece in checkresult:
                                        map[piece[0]][piece[1]] = 0
                                        lifemap[piece[0]][piece[1]] = 0

                                if player1action == 0:
                                    player1action = 2
                                    turn = 3 - turn  # 切换下棋方

                                    allturn += 1

                                    chessablealpha = 0

                            else:
                                tiptext1 = "这里不与我方棋子相邻或已经放置了墙体，无法下棋！"

                    # 玩家1使用E键
                    if event.key == pygame.K_e and turn == 1:
                        place = player1_place[:]
                        numsum = get_neighbors(place[0], place[1], map, turn)
                        if numsum >= 1:
                            if player1action > 0:
                                if is_valid_position(place[0], place[1]):
                                    if player1score >= dglist[dgnum]:
                                        player1score -= dglist[dgnum]
                                        map[place[0]][place[1]] = 0

                                        chessdown = 1

                                        if dgnum == 0:
                                            try:
                                                if map[place[0] - 1][place[1] - 1] != 0:
                                                    if map[place[0] - 1][place[1] - 1] == 2:
                                                        player2life -= 1
                                                        player1score += 1
                                                    elif map[place[0] - 1][place[1] - 1] == 1:
                                                        player1life -= 1
                                                    lifemap[place[0] - 1][place[1] - 1] -= 3
                                            except list.IndexError:
                                                pass
                                            try:
                                                if map[place[0] + 1][place[1] - 1] != 0:
                                                    if map[place[0] + 1][place[1] - 1] == 2:
                                                        player2life -= 1
                                                        player1score += 1
                                                    elif map[place[0] + 1][place[1] - 1] == 1:
                                                        player1life -= 1
                                                    lifemap[place[0] + 1][place[1] - 1] -= 3
                                            except list.IndexError:
                                                pass
                                            try:
                                                if map[place[0] - 1][place[1] + 1] != 0:
                                                    if map[place[0] - 1][place[1] + 1] == 2:
                                                        player2life -= 1
                                                        player1score += 1
                                                    elif map[place[0] - 1][place[1] + 1] == 1:
                                                        player1life -= 1
                                                    lifemap[place[0] - 1][place[1] + 1] -= 3
                                            except list.IndexError:
                                                pass
                                            try:
                                                if map[place[0] + 1][place[1] + 1] != 0:
                                                    if map[place[0] + 1][place[1] + 1] == 2:
                                                        player2life -= 1
                                                        player1score += 1
                                                    elif map[place[0] + 1][place[1] + 1] == 1:
                                                        player1life -= 1
                                                    lifemap[place[0] + 1][place[1] + 1] -= 3
                                            except list.IndexError:
                                                pass

                                        elif dgnum == 1:
                                            player_color = 1

                                            # 向上下左右四个方向出发，将遇到的所有棋子反转阵营，当遇到空白时结束这个方向的检测
                                            directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]  # 上、下、左、右

                                            for dx, dy in directions:
                                                # 初始位置设为玩家放置棋子的位置
                                                x, y = place[0], place[1]

                                                # 尝试向当前方向移动
                                                x += dx
                                                y += dy

                                                # 当前位置在棋盘内并且是敌方棋子
                                                while 0 <= x < BOARD_SIZE and 0 <= y < BOARD_SIZE and map[x][
                                                    y] == 3 - player_color:
                                                    if lifemap[x][y] > 1:
                                                        lifemap[x][y] -= 1
                                                    else:
                                                        # 反转棋子
                                                        map[x][y] = player_color

                                                    # 继续向该方向前进
                                                    x += dx
                                                    y += dy

                                                # 如果当前位置在棋盘内并且是空白格子，表示已经到达可以反转棋子的边界
                                                if 0 <= x < BOARD_SIZE and 0 <= y < BOARD_SIZE and (map[x][y] == 0 or map[x][y] == 1 or map[x][y] == 3):
                                                    # 回退一步，因为最后一步可能越过了目标
                                                    x -= dx
                                                    y -= dy

                                                    # 如果回退后的位置是敌方棋子，那么之前的反转操作是有效的
                                                    if 0 <= x < BOARD_SIZE and 0 <= y < BOARD_SIZE and map[x][
                                                        y] == 3 - player_color:
                                                        # 反转从初始位置到当前位置之间的所有棋子
                                                        while 0 <= x < BOARD_SIZE and 0 <= y < BOARD_SIZE and map[x][y] == 3 - player_color:
                                                            map[x][y] = player_color

                                                            # 继续向回退的方向前进
                                                            x -= dx
                                                            y -= dy

                                                            # 如果回到玩家放置棋子的位置，停止循环
                                                            if x == place[0] and y == place[1]:
                                                                break

                                        elif dgnum == 2:
                                            map[place[0]][place[1]] = 3
                                            lifemap[place[0]][place[1]] += 5

                                        tiptext1 = "玩家1在(" + str(place[0]) + "," + str(place[1]) + ")使用特殊道具"
                                        player1allaction += 1

                                        player1action -= 1

                                        chessablealpha = 0

                                        nownum = dgnum
                                        while dgnum == nownum:
                                            dgnum = random.randint(0, len(dglist) - 1)

                                    else:
                                        tiptext1 = "玩家1使用特殊道具所需得分不足"

                            if player1action == 0:
                                player1action = 2
                                turn = 3 - turn  # 切换下棋方

                                allturn += 1

                                chessablealpha = 0

                        else:
                            tiptext1 = "这里不在我方使用道具的范围内哦！"

                        checkresult = ConsecutiveChessCheck(map)
                        player1score += len(checkresult)
                        player2life -= len(checkresult)
                        for piece in checkresult:
                            map[piece[0]][piece[1]] = 0
                            lifemap[piece[0]][piece[1]] = 0

                    # 玩家2使用 Ctrl 键
                    if (event.key == pygame.K_RCTRL and turn == 2) or (
                            event.key == pygame.K_LCTRL and turn == 2):
                        # 确保 player2_place 已经定义并包含了玩家2想要下棋的位置
                        place = player2_place[:]

                        if is_valid_position(place[0], place[1]):
                            numsum = get_neighbors(place[0], place[1], map, turn)
                            if numsum >= 1 or map[place[0]][place[1]] == 2:
                                if player2action > 0:
                                    if map[place[0]][place[1]] == 1:
                                        player1life -= 1

                                        if lifemap[place[0]][place[1]] > 0:
                                            lifemap[place[0]][place[1]] -= 1

                                    elif map[place[0]][place[1]] == 2:
                                        lifemap[place[0]][place[1]] += 1

                                        player2life += 1

                                        tiptext2 = "玩家2巩固了位于(" + str(place[0]) + "," + str(place[1]) + ")的棋子"

                                    elif map[place[0]][place[1]] == 3:
                                        lifemap[place[0]][place[1]] -= 1

                                    elif map[place[0]][place[1]] == 0:
                                        map[place[0]][place[1]] = turn
                                        lifemap[place[0]][place[1]] = 1

                                        player2life += 1

                                        tiptext2 = "玩家2在(" + str(place[0]) + "," + str(place[1]) + ")下棋"

                                    player2score += 1
                                    player2allaction += 1

                                    chessdown = 1

                                    player2action -= 1

                                    chessablealpha = 0

                                    nownum = dgnum
                                    while dgnum == nownum:
                                        dgnum = random.randint(0, len(dglist) - 1)

                                    checkresult = ConsecutiveChessCheck(map)
                                    player2score += len(checkresult)
                                    player1life -= len(checkresult)
                                    for piece in checkresult:
                                        map[piece[0]][piece[1]] = 0
                                        lifemap[piece[0]][piece[1]] = 0

                                if player2action == 0:
                                    player2action = 2
                                    turn = 3 - turn  # 切换下棋方

                                    allturn += 1

                                    chessablealpha = 0
                            else:
                                tiptext2 = "这里不与我方棋子相邻或已经放置了墙体，无法下棋！"

                    # 玩家2使用Shift键
                    if (event.key == pygame.K_RSHIFT and turn == 2) or (
                            event.key == pygame.K_LSHIFT and turn == 2):
                        place = player2_place[:]
                        numsum = get_neighbors(place[0], place[1], map, turn)
                        if numsum >= 1:
                            if player2action > 0:
                                if is_valid_position(place[0], place[1]):
                                    if player2score >= dglist[dgnum]:
                                        player2score -= dglist[dgnum]
                                        map[place[0]][place[1]] = 0

                                        chessdown = 1

                                        if dgnum == 0:
                                            try:
                                                if map[place[0] - 1][place[1] - 1] != 0:
                                                    if lifemap[place[0] - 1][place[1] - 1] == 1:
                                                        player1life -= 1
                                                        player2score += 1
                                                    elif lifemap[place[0] - 1][place[1] - 1] == 2:
                                                        player2life -= 1
                                                    lifemap[place[0] - 1][place[1] - 1] -= 3
                                            except list.IndexError:
                                                pass
                                            try:
                                                if map[place[0] + 1][place[1] - 1] != 0:
                                                    if lifemap[place[0] + 1][place[1] - 1] == 1:
                                                        player1life -= 1
                                                        player2score += 1
                                                    elif lifemap[place[0] + 1][place[1] - 1] == 2:
                                                        player2life -= 1
                                                    lifemap[place[0] + 1][place[1] - 1] -= 3
                                            except list.IndexError:
                                                pass
                                            try:
                                                if map[place[0] - 1][place[1] + 1] != 0:
                                                    if lifemap[place[0] - 1][place[1] + 1] == 1:
                                                        player1life -= 1
                                                        player2score += 1
                                                    elif lifemap[place[0] - 1][place[1] + 1] == 2:
                                                        player2life -= 1
                                                    lifemap[place[0] - 1][place[1] + 1] -= 3
                                            except list.IndexError:
                                                pass
                                            try:
                                                if map[place[0] + 1][place[1] + 1] != 0:
                                                    if lifemap[place[0] + 1][place[1] + 1] == 1:
                                                        player1life -= 1
                                                        player2score += 1
                                                    elif lifemap[place[0] + 1][place[1] + 1] == 2:
                                                        player2life -= 1
                                                    lifemap[place[0] + 1][place[1] + 1] -= 3
                                            except list.IndexError:
                                                pass

                                        elif dgnum == 1:
                                            player_color = 2

                                            # 向上下左右四个方向出发，将遇到的所有棋子反转阵营，当遇到空白时结束这个方向的检测
                                            directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]  # 上、下、左、右

                                            for dx, dy in directions:
                                                # 初始位置设为玩家放置棋子的位置
                                                x, y = place[0], place[1]

                                                # 尝试向当前方向移动
                                                x += dx
                                                y += dy

                                                # 当前位置在棋盘内并且是敌方棋子
                                                while 0 <= x < BOARD_SIZE and 0 <= y < BOARD_SIZE and map[x][
                                                    y] == 3 - player_color:
                                                    if lifemap[x][y] > 1:
                                                        lifemap[x][y] -= 1
                                                    else:
                                                        # 反转棋子
                                                        map[x][y] = player_color

                                                    # 继续向该方向前进
                                                    x += dx
                                                    y += dy

                                                # 如果当前位置在棋盘内并且是空白格子，表示已经到达可以反转棋子的边界
                                                if 0 <= x < BOARD_SIZE and 0 <= y < BOARD_SIZE and (map[x][y] == 0 or map[x][y] == 2 or map[x][y] == 3):
                                                    # 回退一步，因为最后一步可能越过了目标
                                                    x -= dx
                                                    y -= dy

                                                    # 如果回退后的位置是敌方棋子，那么之前的反转操作是有效的
                                                    if 0 <= x < BOARD_SIZE and 0 <= y < BOARD_SIZE and map[x][
                                                        y] == 3 - player_color:
                                                        # 反转从初始位置到当前位置之间的所有棋子
                                                        while 0 <= x < BOARD_SIZE and 0 <= y < BOARD_SIZE and map[x][y] == 3 - player_color:
                                                            map[x][y] = player_color

                                                            # 继续向回退的方向前进
                                                            x -= dx
                                                            y -= dy

                                                            # 如果回到玩家放置棋子的位置，停止循环
                                                            if x == place[0] and y == place[1]:
                                                                break

                                        elif dgnum == 2:
                                            map[place[0]][place[1]] = 3
                                            lifemap[place[0]][place[1]] += 5

                                        tiptext2 = "玩家2在(" + str(place[0]) + "," + str(place[1]) + ")使用特殊道具"
                                        player2allaction += 1

                                        player2action -= 1

                                        chessablealpha = 0

                                        nownum = dgnum
                                        while dgnum == nownum:
                                            dgnum = random.randint(0, len(dglist) - 1)

                                    else:
                                        tiptext2 = "玩家2使用特殊道具所需得分不足"

                            if player2action == 0:
                                player2action = 2
                                turn = 3 - turn  # 切换下棋方

                                allturn += 1

                                chessablealpha = 0

                        else:
                            tiptext2 = "这里不在我方使用道具的范围内哦！"

                        checkresult = ConsecutiveChessCheck(map)
                        player2score += len(checkresult)
                        player1life -= len(checkresult)
                        for piece in checkresult:
                            map[piece[0]][piece[1]] = 0

                    chessnumtextimg1 = font.render('玩家1当前棋数 ' + str(chessnum[0]), True, ALLCOLOR.red)
                    chessnumtextimg2 = font.render('玩家2当前棋数 ' + str(chessnum[1]), True, ALLCOLOR.blue)
                    turntextimg = font.render('当前是玩家 ' + str(turn) + " 的回合", True, ALLCOLOR.gray)

                    if chessdown == 1:
                        # 计算各方棋子总数为chessnum
                        chessnum = [0, 0]
                        for i in range(BOARD_SIZE):
                            for j in range(BOARD_SIZE):
                                if map[i][j] == 1:
                                    chessnum[0] += 1
                                elif map[i][j] == 2:
                                    chessnum[1] += 1

                        # 获取玩家1能够下棋的坐标并保存到列表BlueCanChessList中
                        BlueCanChessList = []
                        for i in range(BOARD_SIZE):
                            for j in range(BOARD_SIZE):
                                if map[i][j] == 0:
                                    numsum = get_neighbors(i, j, map, 2)
                                    if numsum >= 1:
                                        BlueCanChessList.append([i, j])
                        # 获取玩家2能够下棋的坐标并保存到列表RedCanChessList中
                        RedCanChessList = []
                        for i in range(BOARD_SIZE):
                            for j in range(BOARD_SIZE):
                                if map[i][j] == 0:
                                    numsum = get_neighbors(i, j, map, 1)
                                    if numsum >= 1:
                                        RedCanChessList.append([i, j])

                        # 清除map中lifemap对应值为0的点
                        for i in range(BOARD_SIZE):
                            for j in range(BOARD_SIZE):
                                if lifemap[i][j] == 0:
                                    map[i][j] = 0

                        try:
                            chessnumratio1 = round(chessnum[0] / chessnum[1], 2)
                            chessnumratio2 = round(chessnum[1] / chessnum[0], 2)
                        except ZeroDivisionError:
                            pass

            keydown = 0

            # 玩家1使用WASD控制
            if keys[pygame.K_a]:
                player1_place[0] -= 1
                if player1_place[0] < 0:
                    player1_place[0] = 0
                keydown += 1

            if keys[pygame.K_d]:
                player1_place[0] += 1
                if player1_place[0] >= BOARD_SIZE:
                    player1_place[0] = BOARD_SIZE - 1
                keydown += 1

            if keys[pygame.K_w]:
                player1_place[1] -= 1
                if player1_place[1] < 0:
                    player1_place[1] = 0

            if keys[pygame.K_s]:
                player1_place[1] += 1
                if player1_place[1] >= BOARD_SIZE:
                    player1_place[1] = BOARD_SIZE - 1
                keydown += 1

            # 玩家2使用方向键控制
            if keys[pygame.K_LEFT]:
                player2_place[0] -= 1
                if player2_place[0] < 0:
                    player2_place[0] = 0
                keydown += 1

            if keys[pygame.K_RIGHT]:
                player2_place[0] += 1
                if player2_place[0] >= BOARD_SIZE:
                    player2_place[0] = BOARD_SIZE - 1
                keydown += 1

            if keys[pygame.K_UP]:
                player2_place[1] -= 1
                if player2_place[1] < 0:
                    player2_place[1] = 0
                keydown += 1

            if keys[pygame.K_DOWN]:
                player2_place[1] += 1
                if player2_place[1] >= BOARD_SIZE:
                    player2_place[1] = BOARD_SIZE - 1
                keydown += 1

            if keydown != 0:
                chessablealpha = 255
                chessalphatime = time.time()

        # 绘制游戏界面
        pygame.draw.rect(win, (220, 220, 220), (0, 0, 200, 40))
        pygame.draw.rect(win, (220, 220, 220), (200, 0, 600, 600))
        pygame.draw.rect(win, (80, 80, 80), (0, 40, BOARD_OFFSET, 600))
        mouse_pos = pygame.mouse.get_pos()

        # 遍历地图数组，绘制已有的单元格
        for i in range(BOARD_SIZE):
            for j in range(BOARD_SIZE):
                color = ALLCOLOR.red if map[j][i] == 1 else ALLCOLOR.blue if map[j][i] == 2 else ALLCOLOR.brown if \
                map[j][i] == 3 else None
                if color:
                    pygame.draw.rect(win, color, (BOARD_OFFSET + j * CELL_SIZE, i * CELL_SIZE, CELL_SIZE, CELL_SIZE),
                                     100)

        chessboard_surface.fill((0, 0, 0, 0))

        # 绘制玩家1能够下棋的位置。
        for i in range(len(RedCanChessList)):
            pygame.draw.rect(chessboard_surface, (255, 155, 155, showchessablealpha), (
                BOARD_OFFSET + RedCanChessList[i][0] * CELL_SIZE, RedCanChessList[i][1] * CELL_SIZE, CELL_SIZE,
                CELL_SIZE),100)

        # 绘制玩家2能够下棋的位置。
        for i in range(len(BlueCanChessList)):
            pygame.draw.rect(chessboard_surface, (155, 155, 255, showchessablealpha), (
                BOARD_OFFSET + BlueCanChessList[i][0] * CELL_SIZE, BlueCanChessList[i][1] * CELL_SIZE, CELL_SIZE,
                CELL_SIZE),100)

        # 绘制两个玩家都能下棋的位置
        for i in range(len(BlueCanChessList)):
            for j in range(len(RedCanChessList)):
                if BlueCanChessList[i] == RedCanChessList[j]:
                    pygame.draw.rect(chessboard_surface, (255, 125, 255, showchessablealpha), (
                        BOARD_OFFSET + BlueCanChessList[i][0] * CELL_SIZE, BlueCanChessList[i][1] * CELL_SIZE,
                        CELL_SIZE,
                        CELL_SIZE), 100)

        # 绘制lifemap上的对应生命数。
        for i in range(BOARD_SIZE):
            for j in range(BOARD_SIZE):
                lifemap[j][i] = checkzero(lifemap[j][i])
                if map[j][i] == 0:
                    lifemap[j][i] = 0
                if lifemap[j][i] > 0:
                    lifemaptext = font.render(str(lifemap[j][i]), True, (255, 255, 255))
                    lifemaptext.set_alpha(showchessablealpha)
                    lifemaptext_rect = lifemaptext.get_rect()
                    chessboard_surface.blit(lifemaptext, (
                        BOARD_OFFSET + j * CELL_SIZE + (CELL_SIZE - lifemaptext_rect[2]) / 2,
                        i * CELL_SIZE + (CELL_SIZE - lifemaptext_rect[3]) / 2))

        win.blit(chessboard_surface, (0, 0))

        if chessablealpha == 255:
            showchessablealpha = nonlinear_move(showchessablealpha, chessablealpha, 3, 0.01)
        else:
            showchessablealpha = nonlinear_move(showchessablealpha, chessablealpha, 1, 0.01)

        # 5秒内没有玩家移动操作则设置chessablealpha为0
        if time.time() - chessalphatime >= 5:
            chessablealpha = 0

        # 绘制棋盘的网格线
        for i in range(BOARD_OFFSET, 800, CELL_SIZE):
            pygame.draw.line(win, ALLCOLOR.gray, (i, 0), (i, showlineslong))
        for j in range(0, 600, CELL_SIZE):
            pygame.draw.line(win, ALLCOLOR.gray, (BOARD_OFFSET, j), (200 + showlineslong, j))

        pygame.draw.rect(win, (110, 110, 110), (0, 601, WINDOW_SIZE[0], 40), 200)
        pygame.draw.line(win, ALLCOLOR.gray, (0, 601), (WINDOW_SIZE[0], 601), 1)
        pygame.draw.line(win, ALLCOLOR.gray, (0, 621), (WINDOW_SIZE[0], 621), 2)
        # 绘制下方提示文字
        tiptext1img = font.render('P1 : ' + tiptext1, True, ALLCOLOR.white)
        win.blit(tiptext1img, (10, 601))
        tiptext2img = font.render('P2 : ' + tiptext2, True, ALLCOLOR.white)
        win.blit(tiptext2img, (10, 621))

        try:
            win.blit(chessnumtextimg1, (WINDOW_SIZE[0] - chessnumtextimg1.get_rect()[2], 603))
            win.blit(chessnumtextimg2, (WINDOW_SIZE[0] - chessnumtextimg2.get_rect()[2], 623))

            win.blit(turntextimg, ((BOARD_OFFSET - turntextimg.get_rect()[2]) / 2, 7))
        except NameError:
            chessnumtextimg1 = font.render('玩家1当前棋数 ' + str(chessnum[0]), True, ALLCOLOR.red)
            win.blit(chessnumtextimg1, (WINDOW_SIZE[0] - chessnumtextimg1.get_rect()[2], 603))
            chessnumtextimg2 = font.render('玩家2当前棋数 ' + str(chessnum[1]), True, ALLCOLOR.blue)
            win.blit(chessnumtextimg2, (WINDOW_SIZE[0] - chessnumtextimg2.get_rect()[2], 623))

        if time.time() - showrandomstarttime < 8:
            pygame.draw.line(win, ALLCOLOR.gray, (200, 0), (200, 600), 1)
            turntextimg = font.render('当前是玩家 ' + str(turn) + " 的回合", True, ALLCOLOR.gray)
            turntextimg.set_alpha(int(255 - showrandomalpha))
            win.blit(turntextimg, ((BOARD_OFFSET - turntextimg.get_rect()[2]) / 2, 7))

        # 在玩家所在单元格上绘制边框，突出显示当前选择，玩家坐标为player1_place，玩家2坐标为player2_place
        pygame.draw.rect(win, (0, 0, 0),
                         (BOARD_OFFSET + showplayer1_place[0] * CELL_SIZE, showplayer1_place[1] * CELL_SIZE, CELL_SIZE,
                          CELL_SIZE),
                         2, border_radius=3)
        pygame.draw.rect(win, (0, 0, 0),
                         (BOARD_OFFSET + showplayer2_place[0] * CELL_SIZE, showplayer2_place[1] * CELL_SIZE, CELL_SIZE,
                          CELL_SIZE),
                         2, border_radius=3)

        relplayer1_place[0] = nonlinear_move(showplayer1_place[0], player1_place[0], 8, 0.01)
        relplayer1_place[1] = nonlinear_move(showplayer1_place[1], player1_place[1], 8, 0.01)

        showplayer1_place[0] = round(relplayer1_place[0], 4)
        showplayer1_place[1] = round(relplayer1_place[1], 4)

        relplayer2_place[0] = nonlinear_move(showplayer2_place[0], player2_place[0], 8, 0.01)
        relplayer2_place[1] = nonlinear_move(showplayer2_place[1], player2_place[1], 8, 0.01)

        showplayer2_place[0] = round(relplayer2_place[0], 4)
        showplayer2_place[1] = round(relplayer2_place[1], 4)

        # 绘制左侧提示文字
        pygame.draw.line(win, (100, 100, 100), (0, 84), (200, 84), 2)

        win.blit(tiptext0_1, (3, 90))
        win.blit(tiptext0_2, (3, 120))

        # 特殊道具，因为会被遮挡所以放前面
        pygame.draw.rect(win, ALLCOLOR.gray, (5, 145, 190, 330), 1, border_radius=5)
        win.blit(dgtext1_1, (10, 150))
        pygame.draw.line(win, ALLCOLOR.gray, (10, 178), (186, 178), 1)
        win.blit(boom, (10, 190))
        win.blit(dgtext2_1, (60, 187))
        win.blit(dgtext2_2, (62, 212))

        win.blit(rainbow, (10, 240))
        win.blit(dgtext3_1, (60, 237))
        win.blit(dgtext3_2, (62, 262))

        win.blit(box, (10, 290))
        win.blit(dgtext4_1, (60, 287))
        win.blit(dgtext4_2, (62, 312))

        dgplace = 185 + 50 * dgnum
        showdgplace = nonlinear_move(showdgplace, dgplace, 8, 0.01)
        pygame.draw.rect(win, ALLCOLOR.yellow, (10, showdgplace, 180, 50), 3, border_radius=5)

        if 10 < mouse_pos[0] < 190 and 185 < mouse_pos[1] < 235:
            pygame.draw.rect(win, ALLCOLOR.gray, (5, 185, 190, 50 + 111), 100)
            pygame.draw.rect(win, ALLCOLOR.white, (5, 185, 190, 50 + 111), 1, border_radius=5)
            win.blit(dgtext2_3, (10, 190))
            win.blit(dgtext2_4, (10, 210))
            win.blit(sm1, (40, 185 + 45))
        elif 10 < mouse_pos[0] < 190 and 235 < mouse_pos[1] < 285:
            pygame.draw.rect(win, ALLCOLOR.gray, (5, 240, 190, 110 + 111), 100)
            pygame.draw.rect(win, ALLCOLOR.white, (5, 240, 190, 110 + 111), 1, border_radius=5)
            win.blit(dgtext3_3, (10, 245))
            win.blit(dgtext3_4, (10, 265))
            win.blit(dgtext3_5, (10, 285))
            win.blit(dgtext3_6, (10, 305))
            win.blit(dgtext3_7, (10, 325))
            win.blit(sm2, (40, 260 + 85))
        elif 10 < mouse_pos[0] < 190 and 285 < mouse_pos[1] < 335:
            pygame.draw.rect(win, ALLCOLOR.gray, (5, 290, 190, 50), 100)
            pygame.draw.rect(win, ALLCOLOR.white, (5, 290, 190, 50), 1, border_radius=5)
            win.blit(dgtext4_3, (10, 295))
            win.blit(dgtext4_4, (10, 315))

        # 显示当前已运行的时间，以-时-分-秒表示，被遮挡所以放前面
        nowtime = time.time() - time_start
        nowtimetext = font.render(
            "·" + str(int(nowtime // 3600)) + ' 时 ' + str(int((nowtime % 3600) // 60)) + ' 分 ' + str(
                int(nowtime % 60)) + ' 秒', True, ALLCOLOR.white)
        win.blit(nowtimetext, (15, 515))
        pygame.draw.rect(win, ALLCOLOR.gray, (5, 480, 190, 115), 1, border_radius=5)
        win.blit(timetext, (10, 485))
        win.blit(versontext_1, (15, 535))
        win.blit(versontext_2, (15, 555))
        win.blit(zstext, (30, 575))
        if 30 < mouse_pos[0] < 30 + zstext.get_rect()[2] and 575 < mouse_pos[1] < 575 + zstext.get_rect()[3]:
            pygame.draw.rect(win, ALLCOLOR.black, (44, 399, 202, 230), 1)
            win.blit(zs_big, (45, 400))

        # 游戏规则
        if 3 < mouse_pos[0] < 3 + tiptext0_2.get_rect()[2] and 90 < mouse_pos[1] < 120 + \
                tiptext0_2.get_rect()[3]:
            pygame.draw.rect(win, (100, 100, 100), (1, 95, 198, 479), 200)
            pygame.draw.rect(win, ALLCOLOR.gray, (1, 95, 198, 479), 1, border_radius=5)

            win.blit(tiptext1_1, (3, 100))
            win.blit(tiptext1_2, (3, 120))
            win.blit(tiptext1_3, (3, 140))

            win.blit(tiptext2_1, (3, 170))
            win.blit(tiptext2_2, (3, 190))
            win.blit(tiptext2_3, (3, 210))

            win.blit(tiptext3_1, (3, 240))
            win.blit(tiptext3_2, (3, 260))
            win.blit(tiptext3_3, (3, 280))
            win.blit(tiptext3_4, (3, 300))

            win.blit(tiptext4_1, (3, 330))
            win.blit(tiptext4_2, (3, 350))
            win.blit(tiptext4_3, (3, 370))
            win.blit(tiptext4_4, (3, 390))

            win.blit(tiptext5_1, (3, 420))
            win.blit(tiptext5_2, (3, 440))
            win.blit(tiptext5_3, (3, 460))
            win.blit(tiptext5_4, (3, 480))
            win.blit(tiptext5_5, (3, 500))
            win.blit(tiptext5_6, (3, 520))

            win.blit(tiptext6_1, (3, 550))

        # 玩家文字绘制
        if player1_place != player2_place:
            if abs(player1_place[1] - player2_place[1]) == 1 and player1_place[0] == player2_place[0]:
                win.blit(P1text,
                         (BOARD_OFFSET + showplayer1_place[0] * CELL_SIZE + CELL_SIZE // 2 - P1text.get_width() // 2,
                          showplayer1_place[1] * CELL_SIZE))
                win.blit(P2text,
                         (BOARD_OFFSET + showplayer2_place[0] * CELL_SIZE + CELL_SIZE // 2 - P2text.get_width() // 2,
                          showplayer2_place[1] * CELL_SIZE))
            else:
                if player1_place[1] != 0:
                    # 绘制P1text在玩家1的位置正上方底部距离玩家10个像素位置且相对玩家1居中
                    win.blit(P1text,
                             (
                                 BOARD_OFFSET + showplayer1_place[
                                     0] * CELL_SIZE + CELL_SIZE // 2 - P1text.get_width() // 2,
                                 showplayer1_place[1] * CELL_SIZE - 15))
                else:
                    win.blit(P1text,
                             (
                                 BOARD_OFFSET + showplayer1_place[
                                     0] * CELL_SIZE + CELL_SIZE // 2 - P1text.get_width() // 2,
                                 showplayer1_place[1] * CELL_SIZE))

                if player2_place[1] != 0:
                    # 绘制P2text在玩家2的位置正上方底部距离玩家10个像素位置且相对玩家2居中
                    win.blit(P2text,
                             (
                                 BOARD_OFFSET + showplayer2_place[
                                     0] * CELL_SIZE + CELL_SIZE // 2 - P2text.get_width() // 2,
                                 showplayer2_place[1] * CELL_SIZE - 15))
                else:
                    win.blit(P2text,
                             (
                                 BOARD_OFFSET + showplayer2_place[
                                     0] * CELL_SIZE + CELL_SIZE // 2 - P2text.get_width() // 2,
                                 showplayer2_place[1] * CELL_SIZE))

        else:
            if player1_place[1] + player2_place[1] != 0:
                win.blit(P3text,
                         (BOARD_OFFSET + showplayer1_place[0] * CELL_SIZE + CELL_SIZE // 2 - P3text.get_width() // 2,
                          showplayer1_place[1] * CELL_SIZE - 15))
            else:
                win.blit(P3text,
                         (BOARD_OFFSET + showplayer1_place[0] * CELL_SIZE + CELL_SIZE // 2 - P3text.get_width() // 2,
                          showplayer1_place[1] * CELL_SIZE + 20))

        if turn == 1:
            chessnumratio1img = font.render("玩家1当前回合剩余" + str(player1action) + "步", True, ALLCOLOR.white)
        else:
            chessnumratio1img = font.render("玩家1当前回合剩余0步", True, ALLCOLOR.white)
        if turn == 2:
            chessnumratio2img = font.render("玩家2当前回合剩余" + str(player2action) + "步", True, ALLCOLOR.white)
        else:
            chessnumratio2img = font.render("玩家2当前回合剩余0步", True, ALLCOLOR.white)

        win.blit(chessnumratio1img, (5, 42))
        win.blit(chessnumratio2img, (5, 62))

        # 右侧数据栏
        pygame.draw.rect(win, (80, 80, 80), (800, 0, 400, 600))
        pygame.draw.line(win, ALLCOLOR.gray, (1000, 5), (1000, 595), 2)

        win.blit(r_text_1, (810, 5))

        win.blit(heartimg, (805, 38))
        win.blit(r_text_2, (830, 40))

        # 绘制玩家1血量条
        pygame.draw.rect(win, ALLCOLOR.red, (805, 62, 190 * (showplayer1life / player1maxlife), 20), border_radius=3)
        pygame.draw.rect(win, ALLCOLOR.white, (805, 62, 190, 20), 1, border_radius=3)

        player1lifetext = font.render(str(round(showplayer1life, 1)) + '/' + str(player1maxlife), True, ALLCOLOR.black)
        win.blit(player1lifetext, (810, 62))

        win.blit(heartimg, (805, 88))
        win.blit(r_text_3, (830, 90))

        showplayer1life = nonlinear_move(showplayer1life, player1life, 2, 0.01)

        # 绘制玩家2血量条
        pygame.draw.rect(win, ALLCOLOR.red, (805, 110, 190 * (showplayer2life / player2maxlife), 20), border_radius=3)
        pygame.draw.rect(win, ALLCOLOR.white, (805, 110, 190, 20), 1, border_radius=3)

        player2lifetext = font.render(str(round(showplayer2life, 1)) + '/' + str(player2maxlife), True, ALLCOLOR.black)
        win.blit(player2lifetext, (810, 111))

        win.blit(starimg, (805, 135))
        win.blit(r_text_4, (830, 140))

        showplayer2life = nonlinear_move(showplayer2life, player2life, 2, 0.01)

        pygame.draw.rect(win, ALLCOLOR.yellow, (805, 165, 190 * (showplayer1score / player1maxscore), 20),
                         border_radius=3)
        pygame.draw.rect(win, ALLCOLOR.white, (805, 165, 190, 20), 1, border_radius=3)

        try:
            player1scoretext = font.render(
                str(round(showplayer1score, 1)) + '/' + str(player1maxscore) + "  平均每步" + str(
                    round(player1maxscore / (player1allaction), 1)) + "分", True, ALLCOLOR.black)
            win.blit(player1scoretext, (810, 164))
        except ZeroDivisionError:
            player1scoretext = font.render(str(player1score) + '/' + str(player1maxscore), True, ALLCOLOR.black)
            win.blit(player1scoretext, (810, 164))

        showplayer1score = nonlinear_move(showplayer1score, player1score, 2, 0.01)

        win.blit(starimg, (805, 185))
        win.blit(r_text_5, (830, 190))

        pygame.draw.rect(win, ALLCOLOR.yellow, (805, 210, 190 * (showplayer2score / player2maxscore), 20),
                         border_radius=3)
        pygame.draw.rect(win, ALLCOLOR.white, (805, 210, 190, 20), 1, border_radius=3)

        try:
            player2scoretext = font.render(
                str(round(showplayer2score, 1)) + '/' + str(player2maxscore) + "  平均每步" + str(
                    round(player2maxscore / (player2allaction), 1)) + "分", True, ALLCOLOR.black)
            win.blit(player2scoretext, (810, 210))
        except ZeroDivisionError:
            player2scoretext = font.render(str(player2score) + '/' + str(player2maxscore), True, ALLCOLOR.black)
            win.blit(player2scoretext, (810, 210))

        showplayer2score = nonlinear_move(showplayer2score, player2score, 2, 0.01)

        pygame.draw.rect(win, ALLCOLOR.gray, (805, 240, 190, 355), 1, border_radius=3)
        win.blit(dgrtext0_1, (810, 240))
        win.blit(font.render("当前总第" + str(allturn) + "回合", True, ALLCOLOR.white),(810, 270))
        win.blit(font.render("(平均 "+str(round((time.time() - time_start)/allturn,1))+" 秒/每回合)", True, ALLCOLOR.white),(810,290))
        win.blit(dgrtext1_1, (810, 310))
        win.blit(dgrtext1_2, (810, 330))

        dgrtext1_1 = font.render("玩家1本局共行动" + str(player1allaction) + "步", True, ALLCOLOR.white)
        dgrtext1_2 = font.render("玩家2本局共行动" + str(player2allaction) + "步", True, ALLCOLOR.white)

        # 底部进度栏

        lsnum1 = 1050 * (3 - showratio2) / ((3 - showratio1) + (3 - showratio2))
        lsnum2 = 1050 * (3 - showratio1) / ((3 - showratio1) + (3 - showratio2))
        pygame.draw.rect(win, ALLCOLOR.white, (0, 640, 1050, 30))
        pygame.draw.rect(win, ALLCOLOR.red, (0, 640, lsnum1, 30))
        pygame.draw.rect(win, (110, 135, 255), (1050 - lsnum2, 640, lsnum2, 30), 100)
        win.blit(vs, (lsnum1 - 15, 640))

        ratiotext1 = almidfont.render("P1已达到对方" + str(round(showratio1, 1)) + '倍' + int(showratio1) * "!", True,
                                      ALLCOLOR.white)
        win.blit(ratiotext1, (10, 640))
        ratiotext2 = almidfont.render("P2已达到对方" + str(round(showratio2, 1)) + '倍' + int(showratio2) * "!", True,
                                      ALLCOLOR.white)
        win.blit(ratiotext2, (1050 - ratiotext2.get_rect()[2] - 10, 640))
        showratio1 = nonlinear_move(showratio1, chessnumratio1, 3, 0.01)
        showratio2 = nonlinear_move(showratio2, chessnumratio2, 3, 0.01)

        if time.time() - showrandomstarttime < 7.5:
            pygame.draw.rect(transparent_surface, (78, 78, 78, showrandomalpha),
                             (showrandomx, showrandomy, showrandomwidth, showrandomheight), 300, 10)

            if time.time() - showrandomstarttime < 4:
                turn = random.randint(1, 2)
                randomtext = bigfont.render("随机:本局玩家 " + str(turn) + " 先手", True, (255, 255, 255))
            # 设置字体透明度
            randomtext.set_alpha(showrandomalpha)
            randomtext = pygame.transform.smoothscale(randomtext, (
                int(387 * (showrandomwidth / 500)), int(35 * (showrandomheight / 300))))
            # 在方框正中绘制文字
            transparent_surface.blit(randomtext, (showrandomx + (showrandomwidth - randomtext.get_rect()[2]) / 2,
                                                  showrandomy + (showrandomheight - randomtext.get_rect()[3]) / 2))
            if time.time() - showrandomstarttime > 5:
                showlineslong = nonlinear_move(showlineslong, 600, 1, 0.01)

                showrandomalpha = nonlinear_move(showrandomalpha, 0, 1, 0.01)
                showrandomx = nonlinear_move(showrandomx, 0, 2, 0.01)
                showrandomy = nonlinear_move(showrandomy, 0, 2, 0.01)
                showrandomwidth = nonlinear_move(showrandomwidth, 200, 2, 0.01)
                showrandomheight = nonlinear_move(showrandomheight, 40, 2, 0.01)
        elif time.time() - showrandomstarttime < 8:
            if not running:
                running = True

        # 胜利判断
        buttonblitx, buttonblity = (1050 - showagainbuttonwidth) / 2, (600 - showagainbuttonheight) / 2 + 50

        showagainbuttonwidth = nonlinear_move(showagainbuttonwidth, againbuttonwidth, 5, 0.01)
        showagainbuttonheight = nonlinear_move(showagainbuttonheight, againbuttonheight, 5, 0.01)

        if WIN:
            # 绘制一个透明矩形，屏幕居中显示
            pygame.draw.rect(transparent_surface, (0, 0, 0, 150), (275, 150, 500, 300), 300, border_radius=10)
            # 再来一局(34, 177, 76, 255)
            pygame.draw.rect(transparent_surface, (34, 177, 76, 255),
                             (buttonblitx, buttonblity, showagainbuttonwidth, showagainbuttonheight))
            pygame.draw.rect(transparent_surface, (255, 255, 255, 255),
                             (buttonblitx, buttonblity, showagainbuttonwidth, showagainbuttonheight), 2,
                             border_radius=5)
            ENDtext = pygame.transform.smoothscale(font.render("再来一局", True, ALLCOLOR.white), (
                60 * (showagainbuttonwidth / 180), 18 * (showagainbuttonheight / 30)))
            transparent_surface.blit(ENDtext, (
                435 + (180 - ENDtext.get_rect()[2]) / 2, 335 + (30 - ENDtext.get_rect()[3]) / 2))

        WIN = False

        if buttonblitx < mouse_pos[0] < buttonblitx + againbuttonwidth and buttonblity < mouse_pos[
            1] < buttonblity + showagainbuttonheight:
            INAGAIN = True
            againbuttonwidth = 198
            againbuttonheight = 33
            for event in pygame.event.get():
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if event.button == 1:
                        ENDTHISTURN = True
        else:
            againbuttonwidth = 180
            againbuttonheight = 30

        try:
            if chessnum[0] / chessnum[1] > 3:
                WINTURN = 1
                WIN = True
        except ZeroDivisionError:
            WINTURN = 1
            WIN = True

        try:
            if chessnum[1] / chessnum[0] > 3:
                WINTURN = 2
                WIN = True
        except ZeroDivisionError:
            WINTURN = 2
            WIN = True

        if WINTURN == 1:
            WINCOLOR = (235, 85, 90)

            transparent_surface.blit(P1wintext, (
                (WINDOW_SIZE[0] - P1wintext.get_rect()[2]) / 2, (550 - P1wintext.get_rect()[3]) / 2))
            player1life = 0
            running = False

            if ENDTHISTURN:
                playing = False

        if WINTURN == 2:
            WINCOLOR = (0, 162, 232)

            transparent_surface.blit(P2wintext, (
                (WINDOW_SIZE[0] - P2wintext.get_rect()[2]) / 2, (550 - P2wintext.get_rect()[3]) / 2))
            player2life = 0
            running = False

            if ENDTHISTURN:
                playing = False

        win.blit(transparent_surface, (0, 0))

        if time.time() - showrandomstarttime < 2:
            clock.tick(400)

            end_surface.fill((0, 0, 0, 0))
            showchangeturnwinwidth = nonlinear_move(showchangeturnwinwidth, changeturnwinwidth, 1.5, 0.01)
            pygame.draw.rect(end_surface, WINCOLOR,
                             (showchangeturnwinwidth, 0, 1055 - showchangeturnwinwidth, WINDOW_SIZE[1]), 640)
            win.blit(end_surface, (0, 0))

        pygame.display.flip()

        pygame.display.set_caption(
            "ConsecutiveChess连续棋 " + verson + " (游戏时请切换英文输入法并开启大写锁定) [FPS:" + str(
                int(clock.get_fps())) + "]")

        clock.tick()

    import WINLib
    WINLib.EndAgain(win, end_surface, WINDOW_SIZE, WINCOLOR)

    return WINTURN